package com.xkstream.client.presenter;

import com.xkstream.client.api.ChatroomApi;
import com.xkstream.client.api.ChatroomService;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.base.BasePresenterImpl;
import com.xkstream.client.entity.Admin;
import com.xkstream.client.entity.User;
import com.xkstream.client.rx.RxHelper;
import com.xkstream.client.rx.RxObserver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoyi on 2017/8/24.
 */

public class AnchorInfoPresenter extends BasePresenterImpl implements AnchorInfoContract.Presenter {
    private AnchorInfoContract.View view;
    private ChatroomService chatroomService;

    public AnchorInfoPresenter(AnchorInfoContract.View view) {
        this.view = view;
        chatroomService = ChatroomApi.getInstance().service;
    }

    @Override
    public void getAdminList(String userCode) {
        view.showLoading();
        Map<String, Object> params = new HashMap<>();
        params.put("anchor_user_code", userCode);
        chatroomService.getAdminList(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<List<Admin>>>(this) {
                    @Override
                    protected void onSuccess(BaseData<List<Admin>> userBaseData) {
                        view.hideLoading();
                        if (userBaseData.getCode() == 1) {
                            view.showAdminList(userBaseData.getData());
                        }
                    }

                    @Override
                    protected void onFail(String message) {
                        view.hideLoading();
                    }
                });
    }

    @Override
    public void removeAdmin(String operatorId, String roomId, String userCode) {
        view.showLoading();
        Map<String, Object> params = new HashMap<>();
        params.put("operator", operatorId);
        params.put("roomid", roomId);
        params.put("user_code", userCode);
        chatroomService.setOrRemoveAdmin(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        view.hideLoading();
                        view.showRemoveAdminResult(baseData.getCode(), userCode);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }
}
